/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.sqlverboser;

import java.awt.*;
import javax.swing.*;

import java.sql.*;

import jdbcmanager.app.*;
import jdbcmanager.dbtools.*;
import jdbcmanager.swing.text.*;

/**
 * Shows an SQL command and its native counterpart (see Connection.nativeSQL() )
 * 
 * @author Francisco Morero Peyrona
 */
final class JSQLViewer extends JDialog
{
	JSQLViewer( String sTitle, String sCatalog, String sSQL )
	{
		super( App.getFrameMain(), sTitle, true );

		setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
		setSize( 510, 410 );

		setLocationRelativeTo( App.getFrameMain() );
		init( DbUtil.getConnection( sCatalog ), formatSQLCommand( sSQL ) );
	}

	//-------------------------------------------------------------------------//

	private void init( Connection conn, String sSQL )
	{
		JSplitPane pnlSplit = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
		JPanel     pnlSQL   = new JPanel( new BorderLayout() );
		JPanel     pnlNat   = new JPanel( new BorderLayout() );
		String     sNative  = null;

		try
		{
			sNative = conn.nativeSQL( sSQL );
		}
		catch( SQLException e )
		{
			sNative = "An exception occurred: can not show native SQL";
		}

		JxSQLCodePane txtSQL = new JxSQLCodePane();
		              txtSQL.setEditable( false );
		              txtSQL.setText( sSQL );

		JLabel lblSQL = new JLabel( "SQL sent to JDBC Driver" );
		       lblSQL.setHorizontalAlignment( SwingConstants.CENTER );
		       lblSQL.setOpaque( true );
		       lblSQL.setBackground( Color.blue.darker() );
		       lblSQL.setForeground( Color.white );

		pnlSQL.add( lblSQL, BorderLayout.NORTH );
		pnlSQL.add( new JScrollPane( txtSQL ), BorderLayout.CENTER );

		//------------------------------------------------------------------//

		JxSQLCodePane txtNat = new JxSQLCodePane();
		              txtNat.setEditable( false );
		              txtNat.setText( sNative );

		JLabel lblNat = new JLabel( "Driver interpretation of the original SQL" );
		       lblNat.setHorizontalAlignment( SwingConstants.CENTER );
		       lblNat.setOpaque( true );
		       lblNat.setBackground( Color.blue.darker() );
		       lblNat.setForeground( Color.white );

		pnlNat.add( lblNat, BorderLayout.NORTH );
		pnlNat.add( new JScrollPane( txtNat ), BorderLayout.CENTER );

		// Split pane ------------------------------------------------------
		pnlSplit.setTopComponent( pnlSQL );
		pnlSplit.setBottomComponent( pnlNat );
		pnlSplit.setDividerLocation( getHeight() / 2 - 30 );

		getContentPane().add( pnlSplit, BorderLayout.CENTER );
	}

	private String formatSQLCommand( String sSQL )
	{
		String           sDelim = "FROM;WHERE;ORDER BY;VALUES;LETF JOIN;RIGHT JOIN;INNER JOIN;FULL JOIN;OUTER JOIN"; 
		StringSymbolizer ss     = new StringSymbolizer( sSQL, sDelim, ';', true );
		StringBuffer     sb     = new StringBuffer( sSQL.length() + 20 ); 
		
		while( ss.hasMoreSymbols() )
		{
			String sSymbol = ss.nextSymbol();
			
			if( sDelim.indexOf( sSymbol.toUpperCase() ) > -1 )
				sb.append( "\n   " );
			
			sb.append( sSymbol ).append( ' ' );
		}
		
		return sb.toString();
	}
}